package imdb_db;

import types.*;

import java.io.*;
import java.util.*;
/**
 * this class parse movie form imdb interface: http://www.imdb.com/interfaces
 * @author chenji
 *
 */
public class Parser {
	public void parseMovies( final String file ) throws IOException
  {
      final List<Movie> buffer = new LinkedList<Movie>();
      final List<Movie> b2 = new LinkedList<Movie>();
      BufferedReader fileReader = new BufferedReader(new FileReader(file));
      String line = fileReader.readLine();
      int movieCount = 0;
      while ( line != null )
      {
          if ( line.length() == 0 || line.indexOf( "(TV)" ) != -1 )
          {
              line = fileReader.readLine();
              continue;
          }
          final int t = line.indexOf( '\t' );
          if ( t > 0 )
          {
              final String title = line.substring( 0, t ).trim();
              String year = line.substring( t ).trim();
              if ( year.length() > 4 )
              {
              	year = year.substring( 0, 4 );
              }
              if ( year.length() == 0 || year.charAt( 0 ) == '?'
                  || title.contains( "{" ) || title.startsWith( "\"" ) )
              {
                  line = fileReader.readLine();
                  continue;
              }
              buffer.add( new Movie( title, Integer.parseInt(year) ) );
              movieCount++;
              if (Integer.parseInt(year) >= 2000) 
                b2.add( new Movie( title, Integer.parseInt(year) ) );
          }
          line = fileReader.readLine();
      }
      fileReader.close();
      System.out.println("movies:" + b2.size());
  }

public static void main(String[] args) {
	try {
		new Parser().parseMovies("movies");
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}
